package com.weslide.weslideapp.fragment;

import com.weslide.weslideapp.R;
import com.weslide.weslideapp.activity.MainActivity_de;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainFragment extends WeSlideFragment {
	private static final String TAG = "MainFragment";
	// private View globalView;
	private int containerId = R.id.page_main_mRllMainCantainer;
	private RadioGroup mRdo;
	private RadioButton mRdbScoreStore;
	private RadioButton mRdbUserCenter;
	private ScoreStoreFragment scoreStoreFragment = new ScoreStoreFragment();
	// private SlideFragment slideFragment=new SlideFragment();
	private UserCenterFragment userCenterFragment = new UserCenterFragment();
	private WeSlideFragment mCurrentFragment;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		globalView = inflater.inflate(R.layout.page_main, container, false);
		findViews();
		setListener();
		init();
		// switchToScoreStore();
		Bundle bundle = getArguments();
		boolean isUserCenter = bundle
				.getBoolean(MainActivity_de.EXTRA_IS_USERCENTER);
		if (isUserCenter) {

			switchToUserCenter();
		} else {
			switchToScoreStore();
		}

		return globalView;
	}

	private void findViews() {
		mRdo = (RadioGroup) findViewById(R.id.page_main_mRdg);
		mRdbScoreStore = (RadioButton) findViewById(R.id.page_main_mRdbScoreStore);
		mRdbUserCenter = (RadioButton) findViewById(R.id.page_main_mRdbUserCenter);
	}

	private void init() {
		FragmentTransaction fragmentTransaction = getChildFragmentManager()
				.beginTransaction();
		fragmentTransaction.add(containerId, scoreStoreFragment,
				scoreStoreFragment.TAG);
		fragmentTransaction.add(containerId, userCenterFragment,
				userCenterFragment.TAG);
		fragmentTransaction.commit();
	}

	private void setListener() {
		mRdo.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.page_main_mRdbScoreStore:
					switchToScoreStore();
					break;

				case R.id.page_main_mRdbUserCenter:
					switchToUserCenter();
					break;
				}
			}
		});
	}

	private void switchToScoreStore() {
		mRdbScoreStore.setChecked(true);
		FragmentTransaction transaction = getChildFragmentManager()
				.beginTransaction();
		// transaction.replace(containerId, scoreStoreFragment);
		transaction.show(scoreStoreFragment);
		transaction.hide(userCenterFragment);
		mCurrentFragment = scoreStoreFragment;
		transaction.commit();

	}

	private void switchToUserCenter() {
		mRdbUserCenter.setChecked(true);
		FragmentTransaction transaction = getChildFragmentManager()
				.beginTransaction();
		// transaction.replace(containerId, userCenterFragment);
		transaction.show(userCenterFragment);
		transaction.hide(scoreStoreFragment);
		mCurrentFragment = userCenterFragment;
		transaction.commit();
	}

	public WeSlideFragment getCurrentFragment() {
		return mCurrentFragment;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (mCurrentFragment != null) {
			mCurrentFragment.onActivityResult(requestCode, resultCode, data);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
